package com.tnsw.facebookautoliker.helper.command;

import java.util.ArrayList;

import android.content.Context;

import com.facebook.LoggingBehavior;
import com.facebook.Session;
import com.facebook.Settings;
import com.tnsw.coreutils.MyLog;
import com.tnsw.coreutils.database.RecordBase;
import com.tnsw.facebookautoliker.database.DBManager;
import com.tnsw.facebookautoliker.database.LikePostId;
import com.tnsw.facebookautoliker.helper.FacebookBGManager;
import com.tnsw.facebookautoliker.helper.FacebookCommandObject;
import com.tnsw.facebookautoliker.helper.GeneralManager;

public class DoUnlikePost extends FacebookCommandObject {
	
	private DBManager dbm;
	private GeneralManager gm;
	private String post_id;
	
	private ArrayList<RecordBase> arr = null;

	public DoUnlikePost(Context context, String post_id) {
		super(context);
		this.tag = "DoUnlikePost";
		
		this.dbm = DBManager.getInstance(context);
		this.gm = GeneralManager.getInstance();
		this.post_id = post_id;
		this.setApplyCounter(false);
		
	}
	
	@Override
	protected boolean prepare() {
		this.arr = this.dbm.getRecords(LikePostId.class.getName(), "select * from like_post_id where post_id=?", new String[] {this.post_id}, 0);
		if (this.arr != null) {
			this.setChildrenCommandCount(this.arr.size());
			
		}
		
		return(true);
	}
	
	@Override
	public void execute() {
		//ArrayList<RecordBase> a = this.dbm.getRecords(LikePostId.class.getName(), "select * from like_post_id where post_id=?", new String[] {this.post_id}, 0);
		super.execute();
		
		if (this.arr != null) {
			
			for (RecordBase r : this.arr) {
				LikePostId lp = (LikePostId) r;
				DoUnlikeObject unlike = new DoUnlikeObject(this.context, lp.xid, lp.fp);
				unlike.setParent(this);
				this.gm.addNewQueue(unlike, 2);
			}
			
		} else {
			MyLog.w(tag, "Cannot unline coz annot find any record in like_post_id, may be it's liked by other application, returning 1001");
			this.fbm.updateCommandStatus(this, 1001, null);
		}
		
	}

}
